﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CCH.DAO;
using CCH.Entity;
using System.IO;

public partial class _20_2004_2 : System.Web.UI.Page
{
    public NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

    protected void Page_Load(object sender, EventArgs e)
    {
        //取回功能編號
        string sfu_type = Request.QueryString["type"];
        int sfu_no = new sysfunctionDAO().Get_pathNO(2004, sfu_type);
        ((MasterPage)this.Master).Create_menu(sfu_no);

        if (!this.IsPostBack)
        {
            this.ObjectDataSource1.SelectParameters["alb_no"].DefaultValue = Request.QueryString["alb_no"];
            this.ListView1.DataBind();
        }

        //判斷來自JS 使用_doPostBack(updatePanel,"") 的情況
        if (Request["__EVENTTARGET"] == this.btn_setting.ClientID && String.IsNullOrEmpty(Request["__EVENTARGUMENT"]))
        {
            this.ListView1.DataBind();
        }
        
    }

    protected void btn_del_Click(object sender, EventArgs e)
    {
        albumDAO dao = new albumDAO();

        foreach (var item in this.ListView1.Items)
        {
            int alb_no = (int)this.ListView1.DataKeys[item.DataItemIndex].Values[0];
            int pho_no = (int)this.ListView1.DataKeys[item.DataItemIndex].Values[1];

            CheckBox cb = (CheckBox)item.FindControl("CheckBox1");

            if (cb.Checked)
            {
                photo d = dao.Get_photo(alb_no, pho_no);
                try
                {
                    File.Delete(d.pho_file);
                    File.Delete(d.pho_thumb);
                }
                catch { }
                

                dao.Delete_photo(alb_no, pho_no);        
            }
        }

        dao.Update();

        JsUtil.AlertJs(this, "刪除完成!");

        this.ListView1.DataBind();
    }

    protected void btn_setting_Click(object sender, EventArgs e)
    {
        albumDAO dao = new albumDAO();

        foreach (var item in this.ListView1.Items)
        {
            int alb_no = (int)this.ListView1.DataKeys[item.DataItemIndex].Values[0];
            int pho_no = (int)this.ListView1.DataKeys[item.DataItemIndex].Values[1];

            CheckBox cb = (CheckBox)item.FindControl("CheckBox1");

            if (cb.Checked)
            {
                album d = dao.Get_album(alb_no);
                d.alb_cover = pho_no;
                dao.Update();
                JsUtil.AlertJs(this, "設定完成!");
                break;
            }
        }
        this.ListView1.DataBind();
    }

    

    protected String Get_url(object alb_no)
    {
        return string.Format("2004-1.aspx?mode=edit&ID={0}&type={1}&modal=true&TB_iframe=true&width=800", alb_no.ToString(), Request["type"]);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string sfu_type = Request.QueryString["type"];

        Response.Redirect("2004.aspx?type=" + sfu_type,true);
    }
}